# models.py
from datetime import datetime
from app import db

class Location(db.Model):
    __tablename__ = 'locations'
    
    code = db.Column(db.String(20), primary_key=True, nullable=False)  # 位置编码作为主键
    type = db.Column(db.String(10), nullable=False)  # 类型: shelf/box
    description = db.Column(db.String(200))
    capacity = db.Column(db.Integer)
    created_at = db.Column(db.DateTime, default=datetime.now)

    # 定义与Book的关系（一对多）
    books = db.relationship('Book', backref='location', lazy=True)

class Book(db.Model):
    __tablename__ = 'books'
    id = db.Column(db.Integer, primary_key=True)
    isbn = db.Column(db.String(13), unique=True, nullable=False)
    title = db.Column(db.String(200), nullable=False)
    authors = db.Column(db.String(200))
    publisher = db.Column(db.String(100))
    publish_date = db.Column(db.String(20))
    pages = db.Column(db.Integer)
    price = db.Column(db.Float(20))
    summary = db.Column(db.Text)
    add_time = db.Column(db.DateTime, default=datetime.now)
    # 添加位置关联（外键）
    location_code = db.Column(db.String(20), db.ForeignKey('locations.code'))